<html><head> 
 
 
<title>GLGE</title> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
 
 
<script type="text/javascript" src="glge-compiled.js"></script> 
<style>
body{background-color: #000;color: #fff;font-family:arial}
</style>
</head><body>

<div id="dump"></div>

<div>
<div style="width:900px;margin:auto;position:relative" id="container">
<canvas id="canvas" width="900" 
height="500"></canvas>
<div id="framerate" style="position:absolute; left: 750px; top: 20px; background-color: #000; opacity: 0.5;height: 30px; width: 130px; border-radius: 5px;	-moz-border-radius5px;-webkit-border-radius:5px;border: 1px solid #fff;">
<div id="debug" style="padding: 5px"></div>
</div>
</div>

<script type="text/javascript">



var doc = new GLGE.Document();
doc.onLoad=function(){
//create the renderer
var gameRenderer=new GLGE.Renderer(document.getElementById('canvas'));
gameScene=new GLGE.Scene();
gameScene=doc.getElement("mainscene");
gameRenderer.setScene(gameScene);


var mouse=new GLGE.MouseInput(document.getElementById('canvas'));
var keys=new GLGE.KeyInput();
var mouseovercanvas;
var hoverobj;


function mouselook(){
	if(mouseovercanvas){
		var mousepos=mouse.getMousePosition();
		mousepos.x=mousepos.x-document.getElementById("container").offsetLeft;
		mousepos.y=mousepos.y-document.getElementById("container").offsetTop;
		var camera=gameScene.camera;
		camerarot=camera.getRotation();
		inc=(mousepos.y-(document.getElementById('canvas').offsetHeight/2))/500;
//		var trans=camera.getRotMatrix().x([0,0,-1,1]);
		var trans=GLGE.mulMat4Vec4(camera.getRotMatrix(),[0,0,-1,1]);
		var mag=Math.pow(Math.pow(trans[0],2)+Math.pow(trans[1],2),0.5);
		trans[0]=trans[0]/mag;
		trans[1]=trans[1]/mag;
		camera.setRotX(1.56-trans[1]*inc);
		camera.setRotZ(-trans[0]*inc);
		var width=document.getElementById('canvas').offsetWidth;
		if(mousepos.x<width*0.3){
			var turn=Math.pow((mousepos.x-width*0.3)/(width*0.3),2)*0.005*(now-lasttime);
			camera.setRotY(camerarot.y+turn);
		}
		if(mousepos.x>width*0.7){
			var turn=Math.pow((mousepos.x-width*0.7)/(width*0.3),2)*0.005*(now-lasttime);
			camera.setRotY(camerarot.y-turn);
		}
	}
}

function checkkeys(){
	var camera=gameScene.camera;
	camerapos=camera.getPosition();
	camerarot=camera.getRotation();
	var mat=camera.getRotMatrix();
//	var trans=mat.x([0,0,-1]);
	var trans=GLGE.mulMat4Vec4(mat,[0,0,-1,1]);
	var mag=Math.pow(Math.pow(trans[0],2)+Math.pow(trans[1],2),0.5);
	trans[0]=trans[0]/mag;
	trans[1]=trans[1]/mag;
	var yinc=0;
	var xinc=0;
	if(keys.isKeyPressed(GLGE.KI_M)) {addduck();}
	if(keys.isKeyPressed(GLGE.KI_W)) {yinc=yinc+parseFloat(trans[1]);xinc=xinc+parseFloat(trans[0]);}
	if(keys.isKeyPressed(GLGE.KI_S)) {yinc=yinc-parseFloat(trans[1]);xinc=xinc-parseFloat(trans[0]);}
	if(keys.isKeyPressed(GLGE.KI_A)) {yinc=yinc+parseFloat(trans[0]);xinc=xinc-parseFloat(trans[1]);}
	if(keys.isKeyPressed(GLGE.KI_D)) {yinc=yinc-parseFloat(trans[0]);xinc=xinc+parseFloat(trans[1]);}
	if(keys.isKeyPressed(GLGE.KI_LEFT_ARROW)) {camera.setRotZ(0.5);}
	if(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y)>30) xinc=0;
	if(levelmap.getHeightAt(camerapos.x,camerapos.y+yinc)>30) yinc=0;
	if(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y+yinc)>30){yinc=0;xinc=0;}
		else{
		camera.setLocZ(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y+yinc)+8);
		}
	if(xinc!=0 || yinc!=0){
		camera.setLocY(camerapos.y+yinc*0.05*(now-lasttime));camera.setLocX(camerapos.x+xinc*0.05*(now-lasttime));
	}
}

levelmap=new GLGE.HeightMap("map.png",120,120,-50,50,-50,50,0,50);


var lasttime=0;
var frameratebuffer=60;
start=parseInt(new Date().getTime());
var now;
function render(){
    now=parseInt(new Date().getTime());
	    frameratebuffer=Math.round(((frameratebuffer*9)+1000/(now-lasttime))/10);
	    document.getElementById("debug").innerHTML="Frame Rate:"+frameratebuffer;
    mouselook();
   checkkeys();
   gameRenderer.render();
   lasttime=now;
}
setInterval(render,1);
var inc=0.2;
document.getElementById("canvas").onmouseover=function(e){mouseovercanvas=true;}
document.getElementById("canvas").onmouseout=function(e){mouseovercanvas=false;}
}
doc.load("level.xml");
</script>

</body></html>